package com.swingolf.dao.hrd;

import com.google.appengine.api.datastore.DatastoreService;
import com.google.appengine.api.datastore.DatastoreServiceFactory;
import com.google.appengine.api.datastore.Entity;
import com.google.appengine.api.datastore.EntityNotFoundException;
import com.google.appengine.api.datastore.Key;
import com.google.appengine.api.datastore.KeyFactory;
import com.google.appengine.api.datastore.PreparedQuery;
import com.google.appengine.api.datastore.Query;
import com.swingolf.dao.PlayerDAO;
import com.swingolf.dao.TeamDAO;
import com.swingolf.model.Team;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Repository;
import org.springframework.util.StringUtils;

import java.util.*;

/**
 * The <code>HRDTemperatureDAOImpl</code> provides crud methods for accessing collecting temperatures.
 */
@Repository
public class HRDTeamDAOImpl implements TeamDAO {

  @Autowired PlayerDAO playerDAO;

  @Override
  public void delete(Team team) {
    DatastoreService datastore = DatastoreServiceFactory.getDatastoreService();
    datastore.delete(KeyFactory.createKey("Team", team.getId()));
  }

  @Override
  public void update(Team team) {
    DatastoreService datastore = DatastoreServiceFactory.getDatastoreService();
    Entity temperatureEntity = null;
    try {
      temperatureEntity = datastore.get(KeyFactory.createKey("Team", team.getId()));
    } catch (EntityNotFoundException e) {
      throw new RuntimeException(e.getMessage(), e);
    }
    temperatureEntity.setProperty("Name", team.getName());
    temperatureEntity.setProperty("Players", toId(team.getPlayers()));
    temperatureEntity.setProperty("CountryImageId", team.getCountryImageId());
    Key key = datastore.put(temperatureEntity);
  }

  private String toId(Collection<Long> players) {
    Collection<Long> result = new LinkedList<Long>();
    for (Long id : players) {
      result.add(id);
    }
    return StringUtils.collectionToCommaDelimitedString(result);
  }

  @Override
  public void save(Team team) {
    DatastoreService datastore = DatastoreServiceFactory.getDatastoreService();
    Entity temperatureEntity = new Entity("Team");
    temperatureEntity.setProperty("Name", team.getName());
    temperatureEntity.setProperty("Players", toId(team.getPlayers()));
    temperatureEntity.setProperty("CountryImageId", team.getCountryImageId());
    Key key = datastore.put(temperatureEntity);
    team.setId(key.getId());
  }

  @Override
  public List<Team> findAll() {
    DatastoreService datastore = DatastoreServiceFactory.getDatastoreService();
    Query q = new Query("Team");
    q.addSort("Name", Query.SortDirection.ASCENDING);
    PreparedQuery pq = datastore.prepare(q);
    List<Team> result = new LinkedList<Team>();
    Iterable<Entity> iterable = pq.asIterable();
    //for (Entity entity : iterable) {
    //  datastore.delete(entity.getKey());
    //}
    for (Entity entity : iterable) {
      result.add(new Team(entity.getKey().getId(), (String) entity.getProperty("Name"), fromId(((String) entity.getProperty("Players"))), (Long) entity.getProperty("CountryImageId")));
    }
    return result;

  }

  private Collection<Long> fromId(String ids) {
    String[] idCollection = StringUtils.commaDelimitedListToStringArray(ids);
    Collection<Long> result = new LinkedList<Long>();
    for (String id : idCollection) {
      result.add(Long.parseLong(id));
    }
    return result;
  }

  private void deleteAll() {
    DatastoreService datastore = DatastoreServiceFactory.getDatastoreService();

  }

  @Override
  public Team findById(Long id) {
    Collection<Team> teams = findAll();
    for (Team team : teams) {
      if (team.getId().equals(id)) {
        return team;
      }
    }
    return null;
  }
}
